package anonymouschat.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import anonymouschat.client.Client;
import anonymouschat.service.ServerIdentObject;
import anonymouschat.service.serverEntry;

public class ListOfServers {

	 public static ArrayList<serverEntry> servers = new ArrayList<serverEntry>();
	 Random rand = new Random();
	 FileInputStream fin;

	 public ListOfServers() 
	 {
		 File dir = new File("publicServerIdentities");

		 String[] children = dir.list();
		 if (children == null) {
			 System.err.println("No list of public servers to connect to");
			 System.exit(1);
		     // Either dir does not exist or is not a directory
		 } else {
		     for (int i=0; i<children.length; i++) {
		         // Get filename of file or directory
		         String filename = children[i];
		     	 serverEntry ident = null;
		 			try {
		 				fin = new FileInputStream(filename);
		 			
		     	    ObjectInputStream ois = new ObjectInputStream(fin);
		     	    ident = (serverEntry) ois.readObject();
		     	    servers.add(ident);
		     	    System.out.println("Added " + ident.serverName + " to list of known servers");
		     	    ois.close();
		 			} catch (FileNotFoundException e) {
		 				e.printStackTrace();
		 				System.err.println("Could not load public server identity file for " + filename);
		 			} catch (IOException e) {
		 				e.printStackTrace();
		 				System.err.println("Unable to read serialized public server identity file for " + filename);
		 			} catch (ClassNotFoundException e) {
		 				e.printStackTrace();
		 				System.err.println("Error occured when casting read in serialized public server identity object to serverEntry for " + filename);
		 			}
		     }
		 }
		 

		 //TODO: Pass in real public keys instead of NULL otherwise this will cause errors
	 }
	 
	 public serverEntry getRandomServer() throws Exception
	 {
		 serverEntry se;
		 if (servers.size() == 0)
			 throw new Exception("The directory 'publicServerIdentities' which holds information about how to connect to servers is empty, public server identities are public[servername]Identity.dat files generated when a server is first run");
		 if (Client.serverChain.size() >= servers.size())
			 throw new Exception("Your number of server hops requested for anonymous routing is larger than the number of available servers, please reduce it to at most " + servers.size());
		 do
		 {
		    se = servers.get(rand.nextInt(servers.size()));
		 } while (Client.serverChain.contains(se));
		 return se;
		 
	 }
	 
	 public serverEntry getByName(String name)
	 {
		 for(int i=0; i<servers.size(); i++)
		 {
			 if (servers.get(i).serverName.equalsIgnoreCase(name))
				 return servers.get(i);
		 }
		 return null;
	 }
	 
	
}

